import {Component} from '@angular/core';
import {NzIconService} from 'ng-zorro-antd';
import {TranslateService} from '@ngx-translate/core';
import {MainService} from './public/service/main.service';
import {ActivatedRoute, Router} from '@angular/router';
import {Setting} from './public/setting/setting';
import {Location} from '@angular/common';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  constructor(
    private _iconService: NzIconService,
    private translate: TranslateService,
    private setting: Setting,
    private mainService: MainService,
    private route: ActivatedRoute,
    private location: Location,
    private router: Router
  ) {
    // set iconfonts source ( icons for project & different files' logo )
    this._iconService.fetchFromIconfont({
      scriptUrl: 'https://at.alicdn.com/t/font_1284396_hk47pvsupz.js' // the iconfonts is from `https://www.iconfont.cn/home/index`
    });

    // get params form further and replace url after got params
    const path = location.path().indexOf('?') > 0 ? location.path().split('?')[0] : location.path();
    this.mainService.languageSupport(); // language support
    this.route.queryParams.subscribe(params => {
      if (params && params.projectVersionId && params.accessToken) {
        localStorage.setItem(Setting.storage.accessToken, params.accessToken);
        localStorage.setItem(Setting.storage.projectVersionId, params.projectVersionId);
        Setting.projectVersionId = params.projectVersionId;
        Setting.codeId = params.codeId ? params.codeId : undefined;
        this.router.navigate([path]);
      }
    });
  }
}
